package service

import (
	pb "gitee.com/shuokeyun/item-service/api/v1"
	"gitee.com/shuokeyun/item-service/internal/domain"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func doConvItemReply(item *domain.Item) *pb.GetItemReply {
	if item == nil {
		return nil
	}
	var createdAt, updatedAt *timestamppb.Timestamp
	if !item.CreatedAt.IsZero() {
		createdAt = timestamppb.New(item.CreatedAt)
	}
	if !item.UpdatedAt.IsZero() {
		updatedAt = timestamppb.New(item.UpdatedAt)
	}
	return &pb.GetItemReply{
		Code:             item.Code,
		Brand:            item.Brand,
		PriceArea:        item.PriceArea,
		Name:             item.Name,
		FullName:         item.FullName,
		Status:           int32(item.Status),
		MarketPrice:      item.MarketPrice,
		CostPrice:        item.CostPrice,
		CustomCombo:      int32(item.CustomCombo),
		CanSeparate:      int32(item.CanSeparate),
		CheckedWithItem:  item.CheckedWithItem,
		SexLimit:         int32(item.SexLimit),
		ChildItem:        item.ChildItem,
		RejectItem:       item.RejectItem,
		TestQtyDesc:      item.TestQtyDesc,
		TestQty:          item.TestQty,
		StorageCondition: item.StorageCondition,
		TestMethod:       item.TestMethod,
		ReportTimeDesc:   item.ReportTimeDesc,
		Consumable:       item.Consumable,
		EmptyStomach:     int32(item.EmptyStomach),
		Significance:     item.Significance,
		CategoryId:       item.CategoryId,
		CreatedAt:        createdAt,
		UpdatedAt:        updatedAt,
	}
}

func doConvListItemReply(items []*domain.Item) []*pb.GetItemReply {
	if len(items) == 0 {
		return nil
	}
	list := make([]*pb.GetItemReply, len(items))
	for k, item := range items {
		list[k] = doConvItemReply(item)
	}
	return list
}
